<?php
/**
* 客开运费计算
* @author ldward
* @date 2018.03.26
* @copyright Leemanpaper.com
*/
class ome_expressfee 
{ 

    //#根据区域ID查询，先查县区、再查市、再查省份
    public function getRegion($AxCode,$city,$logi_id,$delivery_time)
    {
        $regionObj = kernel::single('eccommon_regions');

        $region = $regionObj->getList("*", array('local_name' => $city ),0,1);
        $regionIds = explode(',', $region[0]['region_path']);
        foreach($regionIds as $key=>$val){
            if($regionIds[$key] == '' || empty($regionIds[$key])){
                unset($regionIds[$key]);
            }
        }
        $comma_separated = implode(",",  $regionIds);


        $sql="SELECT A.* FROM sdb_ome_expressfee_objects A
            inner join sdb_ome_expressfee B on B.id=A.expressfee_id
             WHERE B.shop_code='".$AxCode."' and B.status='1' and (logistics like '%,".$logi_id.",%' or logistics LIKE '%,".$logi_id."')and A.region_id in (".$comma_separated.")
              and B.effective_time<=".$delivery_time." and B.disable_time> ".$delivery_time;
        $expressfee_objects = kernel::database()->select($sql);
        if(empty($expressfee_objects))
        {
            return null;
        }else{
            return $expressfee_objects;
        }
        // $sql="SELECT A.* FROM sdb_ome_expressfee_objects A
        //     inner join sdb_ome_expressfee B on B.id=A.expressfee_id
        //      WHERE B.shop_code='".$AxCode."' and B.status='1' and logistics like '%".$logi_id."%' and A.region_id=".$region_id."
        //       and B.effective_time<=".$delivery_time." and B.disable_time> ".$delivery_time;
        // $expressfee_objects = kernel::database()->select($sql);
        // if(empty($expressfee_objects))
        // {
        //     $region_objects = kernel::database()->select("SELECT p_region_id,region_grade from sdb_eccommon_regions where region_id=".$region_id);
        //     if(!empty($region_objects) && $region_objects[0]['p_region_id']!='')
        //     {
        //         return $this->getRegion($AxCode,$region_objects[0]['p_region_id'],$logi_id,$delivery_time);
        //     }else{
        //         return null;
        //     }
        // }else{
        //     return $expressfee_objects;
        // }
    }

    //add by ldward 20180607
    // $inparams['logi_id']=$data['logi_id'];
    // $inparams['logi_name']=$logi_name;
    // $inparams['area_id']=$area_id;
    // $inparams['shop_id']=$order['shop_id'];
    // $inparams['weight']=$weight;
    // $inparams['order_id']=$order_id;
    public function AutoCalcExpressfee($inparams)
    {
        $weight=$inparams['weight'];
        $shopModel=app::get('ome')->model('shop');
        $shop_data=$shopModel->getlist('AXCustomer',array('shop_id' => $inparams['shop_id'] ),0,1);
        
        $expressfee_objects=$this->getRegion($shop_data[0]['AXCustomer'],$inparams['ship_city'],$inparams['logi_id'],$inparams['delivery_time']);
        if(empty($expressfee_objects))
        {
           return 0; 
        }

        $firstunit = $expressfee_objects[0]['firstunit'];
        $continueunit = $expressfee_objects[0]['continueunit'];
        $firstprice = $expressfee_objects[0]['firstprice'];
        $continueprice = $expressfee_objects[0]['continueprice'];
        $dt_expressions = $expressfee_objects[0]['dt_expressions'];
        
        if($dt_expressions && $dt_expressions != '')
        {
            $price = utils::cal_fee($dt_expressions, $weight, 0,$firstprice,$continueprice); //TODO 生成快递费用
        }else{
            if($continueunit>0 && bccomp($weight,$firstunit,3) == 1 )
            {
                $continue_price = (($weight-$firstunit)/$continueunit)*$continueprice;

            }else{
                $continue_price = 0;
            }
            $price = $firstprice+$continue_price;
        }
        return $price;
    }
}

